﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using Org.LLRP.LTK.LLRPV1.Impinj;
using RFID;
using System;
using System.Collections.Generic;

namespace ConfExpert.Forms.Depo.Cikis
{
    public partial class OPonkabul : XForm
    {
        char tip;
        public OPonkabul(char _tip)
        {
            InitializeComponent();
            tip = _tip;
            if (tip == 'T')
                txtInfo.Caption = "Dönen top girişi";
            else
            {
                txtInfo.Caption = "Boş rulo girişi";
                lblNotes.Visible = meNotes.Visible = false;
            }
        }

        private void OPlist_Load(object sender, EventArgs e)
        {
            btnBaslat.Click += new EventHandler(btnBaslat_Click);
            btnBitir.Click += new EventHandler(btnBitir_Click);

            btnBaslat.Enabled = true;
            meOkunan.Enabled = btnBitir.Enabled = false;

            StartRFID();
        }

        void btnBitir_Click(object sender, EventArgs e)
        {
            ExecuteNonQuery("UpdOPlist", tip, meOkunan.Text.Replace(Environment.NewLine, ","), meNotes.Text);
            Notify("Kaydedildi.");

            btnBaslat.Enabled = true;
            meOkunan.Enabled = btnBitir.Enabled = false;
            meGozardi.Text = meOkunan.Text = meNotes.Text = string.Empty;
            tags.Clear();
        }

        void btnBaslat_Click(object sender, EventArgs e)
        {
            meOkunan.Enabled = btnBitir.Enabled = true;
            btnBaslat.Enabled = false;
        }

        public void StartRFID()
        {
            // format : IPADRESS,Power:Sensitivity
            //192.168.1.27
            string ip1 = AppSession.GateIP + ",87:1";
            TagManager tm = new TagManager(ENUM_ImpinjInventorySearchType.Single_Target_With_Suppression, ip1);
            tm.TagRead += new TagManager.TagReadHandler(tm_TagRead);
            tm.BoundForm = this;
        }

        List<int> tags = new List<int>();
        void tm_TagRead(Tag tag, double rssi)
        {
            if (!tags.Contains(tag.Id))
            {
                tags.Add(tag.Id);
                if (btnBitir.Enabled)
                    meOkunan.Text += tag.Id.ToString() + Environment.NewLine;
                else
                    meGozardi.Text += tag.Id.ToString() + Environment.NewLine;
            }
        }

    }
}